/*
package com.cyt.sys.util;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComFailException;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import org.apache.log4j.Logger;

import java.io.File;

*/
/**
 * Created by sunli on 2019/4/11.
 *//*


public class ToPDF {


    private static final Logger LOG = Logger.getLogger(ToPDF.class);

    private static final int wdFormatPDF = 17; // PDF 格式
    private static final int xlTypePDF = 0;  // xls格式

    public boolean toPDF(String sfileName, String toFileName) {
        System.out.println("------开始转换------");
        String suffix = getFileSufix(sfileName);
        File file = new File(sfileName);
        if (!file.exists()) {
            System.out.println("文件不存在！");
            return false;
        }
        if (suffix.equals("pdf")) {
            System.out.println("PDF not need to convert!");
            return false;
        }

        if (suffix.equals("doc") || suffix.equals("docx") || suffix.equals("txt")) {
            return word2PDF(sfileName, toFileName);
        } else if (suffix.equals("ppt") || suffix.equals("pptx")) {
            return ppt2PDF(sfileName, toFileName);
        } else if (suffix.equals("xls") || suffix.equals("xlsx")) {
            return excel2PDF(sfileName, toFileName);
        } else {
            System.out.println("文件格式不支持转换!");
            return false;
        }

    }

    //截取文件后缀方法
    public static String getFileSufix(String fileName) {
        int splitIndex = fileName.lastIndexOf(".");
        return fileName.substring(splitIndex + 1);
    }

    //转换word文档
    public boolean word2PDF(String sfileName, String toFileName) {
        long start = System.currentTimeMillis();
        ActiveXComponent app = null;
        Dispatch doc = null;
        boolean result = true;

        try {
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", new Variant(false));
            Dispatch docs = app.getProperty("Documents").toDispatch();
            doc = Dispatch.call(docs, "Open", sfileName).toDispatch();
            System.out.println("打开文档..." + sfileName);
            System.out.println("转换文档到 PDF..." + toFileName);
            File tofile = new File(toFileName);
            if (tofile.exists()) {
                tofile.delete();
            }

            Dispatch.call(doc, "SaveAs", toFileName, wdFormatPDF);
            long end = System.currentTimeMillis();
            System.out.println("转换完成..用时：" + (end - start) + "ms.");

            result = true;
        } catch (Exception e) {
            // TODO: handle exception

            System.out.println("========Error:文档转换失败：" + e.getMessage());
            result = false;
        } finally {
            Dispatch.call(doc, "Close", false);
            System.out.println("关闭文档");
            if (app != null) {
                app.invoke("Quit", new Variant[] {});
            }
        }

        ComThread.Release();

        return result;
    }
    //转换excel文档
    public boolean excel2PDF(String inputFile, String pdfFile) {
        ActiveXComponent app = null;
        Dispatch excel = null;
        boolean result = true;
        try {

            app = new ActiveXComponent("Excel.Application");
            app.setProperty("Visible", false);
            Dispatch excels = app.getProperty("Workbooks").toDispatch();
            excel = Dispatch.call(excels, "Open", inputFile, false, true).toDispatch();
            Dispatch.call(excel, "ExportAsFixedFormat", xlTypePDF, pdfFile);
            System.out.println("打开文档..." + inputFile);
            System.out.println("转换文档到 PDF..." + pdfFile);
            result = true;
        } catch (Exception e) {
            result = false;
        } finally {
            if (excel != null) {
                Dispatch.call(excel, "Close");
            }
            if (app != null) {
                app.invoke("Quit");
            }
        }
        return result;
    }
    //转换ppt文档
    public boolean ppt2PDF(String srcFilePath, String pdfFilePath) {
        ActiveXComponent app = null;
        Dispatch ppt = null;
        boolean result = true;
        try {
            ComThread.InitSTA();
            app = new ActiveXComponent("PowerPoint.Application");
            Dispatch ppts = app.getProperty("Presentations").toDispatch();

            // 因POWER.EXE的发布规则为同步，所以设置为同步发布
            ppt = Dispatch.call(ppts, "Open", srcFilePath, true, // ReadOnly
                    true, // Untitled指定文件是否有标题
                    false// WithWindow指定文件是否可见
            ).toDispatch();

            Dispatch.call(ppt, "SaveAs", pdfFilePath, 32); // ppSaveAsPDF为特定值32
            System.out.println("转换文档到 PDF..." + pdfFilePath);
            result = true; // set flag true;
        } catch (ComFailException e) {
            result = false;
        } catch (Exception e) {
            result = false;
        } finally {
            if (ppt != null) {
                Dispatch.call(ppt, "Close");
            }
            if (app != null) {
                app.invoke("Quit");
            }
            ComThread.Release();
        }
        return result;
    }

    public static void main(String[] args) {
        ToPDF d = new ToPDF();
        //d.wordToPDF("E:\\poi-test\\hadoop集群搭建.docx", "E:\\poi-test\\hadoop集群搭建.pdf");
        d.toPDF("E:\\Test\\新建 Microsoft Word 文档.docx", "E:\\Test\\移动校园说明书（学生）.pdf");
    }
}
*/
